<?php

namespace app;

use think\facade\Db;

/**
 * 数据操作类
 */
class Db2
{
    static private $use_where_befor = true;

    /**
     * Db2 constructor. 构造函数
     * @param array $config
     */
    public function __construct($config = [])
    {
        self::$use_where_befor = true;

        if (isset($config['where_befor'])) {
            self::$use_where_befor = $config['where_befor'];
        }
        // die($name);
    }

    //添加单条数据 并返回id
    static public function insert_data($name, $data)
    {
        return Db::name($name)->insertGetId($data);
    }

    // 添加多条数据，返回添加成功的条数
    static public function insert_datalist($name, $data)
    {
        return Db::name($name)->insertAll($data);
    }

    // 查询单条数据
    static public function get_data($name, $where_list = null, $field = null)
    {
        $data = Db::name($name);

        if (self::$use_where_befor) {
            $data = $data->where('isdel', 0);
        }

        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        if ($field) {
            $data = $data->field($field);
        }
        // print_r(Db::getLastSql());die();

        return $data->find();
    }

    // 获取数据的条数
    static public function get_count($name, $where_list = null)
    {
        $data = Db::name($name);

        if (self::$use_where_befor) {
            $data = $data->where('isdel', 0);
        }

        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        $num = $data->count();

        return $num;
    }

    // 查询多条数据
    static public function get_datalist($name, $where_list = null, $field = null, $order = null, $limit = null, $distinct = false, $whereor = null)
    {
        $data = Db::name($name);

        if ($whereor) {
            $data = $data->whereOr($whereor);
        } else {
            if (self::$use_where_befor) {
                $data = $data->where('isdel', 0);
            }

            if ($where_list) {
                foreach ($where_list as $where) {
                    $data = $data->where($where);
                }
            }
        }
        if ($field) {
            $data = $data->field($field);
        }

        if ($order) {
            $data = $data->order($order);
        }

        if ($limit) {
            $data = $data->limit($limit);
        }

        if ($distinct) {
            $data = $data->distinct($distinct);
        }

        $data = $data->select()->toArray();
//         print_r(Db::getLastSql());die();

        return $data;
    }

    // 多表关联查询多条数据
    static public function get_join_datalist($name, $where_list = null, $join = null, $field = null, $order = null, $limit = null, $group = null)
    {
        $name_arr = explode(' ', $name);
        $data = Db::name($name_arr[0]);

        $data = $data->alias($name_arr[1]);

        // print_r($join);die();
        if ($join) {
            foreach ($join as $key => $val) {
                $data = $data->join($key, $join[$key]);
            }
        }
        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        if ($field) {
            $data = $data->field($field);
        }

        if ($order) {
            $data = $data->order($order);
        }

        if ($limit) {
            $data = $data->limit($limit);
        }

        if ($group) {
            $data = $data->group($group);
        }

        $data = $data->select()->toArray();
         print_r(Db::getLastSql());die();
        return $data;
    }

    // 多表关联查询多条数据
    static public function get_join_datalist2($name, $join = null, $where_list = null, $field = null, $order = null, $limit = null, $group = null)
    {
        $name_arr = explode(' ', $name);
        $data = Db::name($name_arr[0]);

        $data = $data->alias($name_arr[1]);

        if (self::$use_where_befor) {
            $data = $data->where('isdel', 0);
        }

        if ($join) {
            foreach ($join as $key => $val) {
                $data = $data->join($key, $join[$key]);
            }
        }
        // print_r($where);die();
        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        if ($field) {
            $data = $data->field($field);
        }

        if ($order) {
            $data = $data->order($order);
        }

        if ($limit) {
            $data = $data->limit($limit);
        }

        if ($group) {
            $data = $data->group($group);
        }

        $data = $data->select();
        // print_r(Db::getLastSql());die();
        return $data;
    }

    // 查询多条数据的分页列表
    static public function get_datalist_page($name, $where_list = null, $curpage = null, $listnum = null, $field = null, $order = null, $whereor = null, $wherenotnull = null)
    {
        $data = Db::name($name);
        if ($whereor) {
            $data = $data->whereOr($whereor);
        } else {
            if (self::$use_where_befor) {
                $data = $data->where('isdel', 0);
            }
            if ($where_list) {
                foreach ($where_list as $where) {
                    $data = $data->where($where);
                }
            }
        }
        if ($wherenotnull) {
            $data = $data->where($wherenotnull, 'not null');
        }

        $total = $data->count();

        if ($field) {
            $data = $data->field($field);
        }

        if ($curpage) {
            $data = $data->page($curpage);
        }

        if ($listnum) {
            $data = $data->limit($listnum);
        }

        if ($order) {
            $data = $data->order($order);
        }

        $data = $data->select()->toArray();
//         print_r([$listnum]);die();
//         print_r(Db::getLastSql());die();
        $pages = ceil($total / $listnum);

        $arr = array('pages' => $pages, 'datalist' => $data, 'total' => $total);

        return $arr;
    }

    // 更新数据
    static public function update_data($name, $where_list = null, $update = null)
    {
        $data = Db::name($name);

        if (self::$use_where_befor) {
            $data = $data->where('isdel', 0);
        }

        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        return $data->update($update);
    }

    // 删除数据
    static public function delete_data($name, $where_list)
    {
        $data = Db::name($name);

        if ($where_list) {
            foreach ($where_list as $where) {
                $data = $data->where($where);
            }
        }

        return $data->where($where)->update(['isdel' => 1]);
    }
}